﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using System.Web;
using Microsoft.SharePoint;
using Slam.FieldTypes;

namespace SlamCms.SharePoint.FieldTypes
{
	public partial class CKEditorFieldEditor : UserControl, IFieldEditor
	{	
		#region IFieldEditor Members

		public bool DisplayAsNewSection
		{
			get { return true; }
		}

		public void InitializeWithField(Microsoft.SharePoint.SPField field)
		{
            SlamField ckEditorField = field as SlamField;

			if (!Page.IsPostBack)
			{
				SPSite siteCollection = SPControl.GetContextSite(HttpContext.Current);

				foreach (SPWeb site in siteCollection.AllWebs)
				{
					foreach (SPList list in site.Lists)
					{
						if (!list.Hidden && (list.BaseTemplate == SPListTemplateType.DocumentLibrary||list.BaseTemplate == SPListTemplateType.PictureLibrary))
						{
							if (list.BaseTemplate == SPListTemplateType.DocumentLibrary)
								ddlDocumentLibraryList.Items.Add(new ListItem((String.IsNullOrEmpty(site.Name) ? "[Root]" : site.Name) + " - " + list.Title, ("/" + site.ServerRelativeUrl.TrimStart('/')).TrimEnd('/') + ("/" + list.RootFolder.Url).TrimEnd('/') + "/"));
                            if (list.BaseTemplate == SPListTemplateType.PictureLibrary)
                                ddlImageLibraryList.Items.Add(new ListItem((String.IsNullOrEmpty(site.Name) ? "[Root]" : site.Name) + " - " + list.Title, ("/" + site.ServerRelativeUrl.TrimStart('/')).TrimEnd('/') + ("/" + list.RootFolder.Url).TrimEnd('/') + "/"));
						}
					}
					site.Dispose();
				}

				ddlDocumentLibraryList.Items.Insert(0, new ListItem("--Select--", ""));
				ddlImageLibraryList.Items.Insert(0, new ListItem("--Select--", ""));

				if (ckEditorField != null)
				{
					string configFileName = (string)ckEditorField.GetCustomPropertyValue("ConfigFileName");
					string selectedPictureLibraryUrl = (string)ckEditorField.GetCustomPropertyValue("ImageUploadPath");
					string selectedDocumentLibraryUrl = (string)ckEditorField.GetCustomPropertyValue("DocumentUploadPath");

					txtConfigFile.Text = configFileName;

					ListItem selectedPictureLibraryItem = ddlImageLibraryList.Items.FindByValue(selectedPictureLibraryUrl);
					if (selectedPictureLibraryItem != null)
						selectedPictureLibraryItem.Selected = true;

					ListItem selectedDocumentLibraryItem = ddlDocumentLibraryList.Items.FindByValue(selectedDocumentLibraryUrl);
					if (selectedDocumentLibraryItem != null)
						selectedDocumentLibraryItem.Selected = true;
				}
			}
		}

		public void OnSaveChange(Microsoft.SharePoint.SPField field, bool isNewField)
		{
			SlamField ckEditorField = field as SlamField;

			if (ddlDocumentLibraryList.SelectedIndex > 0)
			{
				ckEditorField.SetCustomProperty("DocumentUploadPath", typeof(string), ddlDocumentLibraryList.SelectedValue);
			}
			if (ddlImageLibraryList.SelectedIndex > 0)
			{
				ckEditorField.SetCustomProperty("ImageUploadPath", typeof(string), ddlImageLibraryList.SelectedValue);
			}
			ckEditorField.SetCustomProperty("ConfigFileName", typeof(string), txtConfigFile.Text);
			ckEditorField.SetRichTextMode(saveAsRichTextCheck.Checked, SPRichTextMode.FullHtml);
		}

		#endregion

	}
}
